﻿using EF.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EF.Controllers
{
    public class ReportControllerController : Controller
    {
        EShopV10 db = new EShopV10();

        public ActionResult Index(String From = "01/01/1900", String To = "12/31/2099")
        {
            DateTime MinDate, MaxDate;
            try
            {
                MinDate = Convert.ToDateTime(From);
            }
            catch
            {
                MinDate = DateTime.MinValue;
            }
            try
            {
                MaxDate = Convert.ToDateTime(To);
            }
            catch
            {
                MaxDate = DateTime.MaxValue;
            }

            var model = db.OrderDetails
                .Where(d => d.Order.OrderDate >= MinDate && d.Order.OrderDate <= MaxDate)
                .GroupBy(d => d.Product.Category)
                .Select(g => new ReportInfo
                {
                    Group = g.Key.NameVN,
                    Revenue = g.Sum(d => d.Quantity * d.UnitPrice),
                    Amount = g.Sum(d => d.Quantity),
                    MinPrice = g.Min(d => d.UnitPrice),
                    MaxPrice = g.Max(d => d.UnitPrice),
                    AvgPrice = g.Average(d => d.UnitPrice)
                });

            return View(model);
        }
	}
}